package edu.uwosh.cs342.group1;

import java.util.ArrayList;
import java.util.List;

import edu.uwosh.cs342.group1.database.DbCloudHelper;
import edu.uwosh.cs342.group1.storage.*;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;

public class QuizAppActivity extends Activity {
	 DbCloudHelper db;
	 ListView listView;
	 Quiz[] quizArray;
	 Quiz currentQuiz;
	 String username;
	 int quizID;
	 ArrayAdapter<Quiz> arrayAdapt;
    /** Called when the activity is first created. */
    @Override
   
    
    public void onCreate(Bundle savedInstanceState) {
    	username = this.getIntent().getStringExtra("User");    	
        super.onCreate(savedInstanceState);
        System.err.println("Create Ran");
        setContentView(R.layout.main);
        
        db = new DbCloudHelper();
        
        listView = (ListView)findViewById(R.id.listView1);
        
        List<Quiz> quizzes = db.GetAvailableQuizzes(username);
        if(quizzes!=null && quizzes.size()>0) {
        	quizArray = new Quiz[1];
        	quizArray = quizzes.toArray(quizArray);
        	
        	arrayAdapt = new ArrayAdapter<Quiz>(this,R.layout.quiz_layout,
        			quizArray);
        	listView.setAdapter(arrayAdapt);
        	listView.setOnItemClickListener(new OnItemClickListener() {
				
				
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					currentQuiz = (Quiz) parent.getItemAtPosition(position);
					quizID = currentQuiz.getId();
					currentQuiz = db.GetQuiz(currentQuiz.getId());
					//DisplayContact.id = contact.getID();
					if(currentQuiz == null){
						System.out.println("DB ERROR!!!!");
					}
					Question q = currentQuiz.getQuestions().get(0);
					ArrayList<Question> questions = new ArrayList<Question>(currentQuiz.getQuestions());
					if(q.isMultipleChoice()) {
						Intent i = new Intent(QuizAppActivity.this,
						MultipleChoiceDisplay.class);
						i.putExtra("questions", questions);
						i.putExtra("correct", 0);
						i.putExtra("total", 0);
						i.putExtra("username",username);
						i.putExtra("quizName", currentQuiz.getName());
						i.putExtra("QuizId",quizID);
						QuizAppActivity.this.startActivity(i);
						System.out.println("MultipleChoice first!");
					}else if(q.isTrueFalse()){
						Intent i = new Intent(QuizAppActivity.this,
								TrueFalseDisplay.class);
						i.putExtra("questions", questions);
						i.putExtra("correct", 0);
						i.putExtra("total", 0);
						i.putExtra("username",username);
						i.putExtra("quizName", currentQuiz.getName());
						i.putExtra("QuizId",quizID);
						QuizAppActivity.this.startActivity(i);
						System.out.println("TrueFalse first!");
					} else if(q.isShortAnswer()) {
						Intent i = new Intent(QuizAppActivity.this,
								ShortAnswerDisplay.class);
						i.putExtra("questions", questions);
						i.putExtra("correct", 0);
						i.putExtra("total", 0);
						i.putExtra("username",username);
						i.putExtra("quizName", currentQuiz.getName());
						i.putExtra("QuizId",quizID);
						QuizAppActivity.this.startActivity(i);
						System.out.println("ShortAnswer first!");
					}
					//QuizAppActivity.this.finish();
					
				}
			});
        }
    }
    
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.quizappscores, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
    	Intent i = null;
        switch (item.getItemId()) {
            case R.id.viewUserScores:
            	i = new Intent(QuizAppActivity.this, UserScoresActivity.class);
    			i.putExtra("User", username);
    	    	QuizAppActivity.this.startActivity(i);
                return true;
            case R.id.viewAllScores:
            	i = new Intent(QuizAppActivity.this, AllScoresActivity.class);
            	QuizAppActivity.this.startActivity(i);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
        
    }
    
    public void onResume(){
    	super.onResume();
    	System.err.println("Resume Ran");
    	List<Quiz> quizzes = db.GetAvailableQuizzes(username);
    	if(quizzes!=null && quizzes.size()>0) {
        	quizArray = new Quiz[1];
        	quizArray = quizzes.toArray(quizArray);
        	arrayAdapt = new ArrayAdapter<Quiz>(this,R.layout.quiz_layout,
        			quizArray);
        	listView.setAdapter(arrayAdapt);
    	} else {
    		AlertDialog.Builder alert = new AlertDialog.Builder(this);
    		alert.setTitle("No more quizzes to take");
    		alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int whichButton) {
					// TODO Auto-generated method stub
					finish();
				}
			});
    		alert.show();
    	}
    	
    }
    
    public void onPause(){
    	super.onPause();
    	System.err.println("Pause Ran");
    }
    
    public void onRestoreInstanceState(Bundle savedInstanceState) {
    	super.onRestoreInstanceState(savedInstanceState);
    	System.err.println("Restore Ran");
    	
    }
    
    String getUsername(){
    	return username;
    }
}